package isg3.data;

import isg3.domain.Concurso;
import isg3.utils.UIDGenerator;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class JDBCConcursoDAO implements IConcursoDAO {
	
	private Connection conn;
	
	public JDBCConcursoDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public void insert(Concurso c) {
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO concursos ( OIDConcurso, titulo, descripcion, estado, fechaIns, fechaCon) VALUES (?, ?, ?, ?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, c.getTitulo());
            stmt.setString(3, c.getDescripcion());
            stmt.setString(4, c.getEstado());
            stmt.setString(5, "");
            stmt.setString(6, "");
    

            stmt.executeUpdate();

            //insert details
           // ddao.insert(conn, o.getDetails(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

	}

	public Concurso select(String OIDConcurso) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Concurso c=null;
        
        try {

            String sql = "SELECT * FROM   usuariosporconcurso WHERE concursoOID = ?";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, OIDConcurso);
            stmt.executeQuery();
            result = stmt.executeQuery();

            result.next();
            c = new Concurso();
           // c.setIdConcurso(result.getString("concursoOID"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally { 
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return c;
        
        	
	
		
	}

	public List selectAllConcursos() {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM concursos"; //WHERE (tipo= tipoU)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            //stmt.setString(4, tipo);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Concurso temp = new Concurso();
                temp.setTitulo(result.getString("titulo"));
                temp.setEstado(result.getString("estado"));
                temp.setDescripcion(result.getString("descripcion"));
                temp.setFechaConcurso(result.getDate("fechaIns"));
                temp.setFechaInscripcion(result.getDate("fechaCon"));
                temp.setIdConcurso(result.getString("OIDConcurso"));
        
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}
	public void updateEstado(Concurso p, String est) {
		
        PreparedStatement stmt = null;
        String sql = "UPDATE concursos SET estado = ? WHERE titulo = ?";
        try {
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, est);
            stmt.setString(2, p.getTitulo());
            
            stmt.executeUpdate();

            //insert details
           // ddao.insert(conn, o.getDetails(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	
	public void updateConcurso(java.util.Date fi,java.util.Date fc, String premio, String idC){
		PreparedStatement stmt = null;
        String sql = "UPDATE Concursos SET fechaIns = ?, fechaCon = ?, premio = ? WHERE OIDConcurso = ?";
        try {
            stmt = conn.prepareStatement(sql);
            java.util.Date myDate= fi ;
            java.util.Date myDate2 = fc;
            stmt.setDate(1, new java.sql.Date( myDate.getTime()));
            stmt.setDate(2, new java.sql.Date(myDate2.getTime()));
            stmt.setString(3, premio);
            stmt.setString(4, idC);
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
   	}

	
}
